package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.banshengyanyu.bottomtrackviewlib.TrackModel;
import com.banshengyanyu.bottomtrackviewlib.interfaces.ClipVideoListener;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;

import flc.ast.BaseEditVideoFragment;
import flc.ast.R;
import flc.ast.activity.VideoEditActivity;
import flc.ast.databinding.FragmentVideoTailorBinding;
import flc.ast.utils.Content;
import stark.common.basic.utils.MediaUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link VideoTailorFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class VideoTailorFragment extends BaseEditVideoFragment<FragmentVideoTailorBinding> implements ClipVideoListener {

    public static final int INDEX = Content.INDEX_TAILOR;
    private long tailorStartTime;
    private long tailorEndTime;

    public static VideoTailorFragment newInstance() {
        VideoTailorFragment fragment = new VideoTailorFragment();
        return fragment;
    }


    @Override
    public void onShow() {
        mVideoEditActivity.mode = VideoEditActivity.MODE_TAILOR;

        mDataBinding.bottomTrackView.setClipVideoMode(TrackModel.ClipVideoMode.OPERATION);
        mDataBinding.bottomTrackView.setMode(TrackModel.ClipMode.CLIP);
        mDataBinding.bottomTrackView.initInfo(mVideoEditActivity.getMainPath(), MediaUtil.getDuration(mVideoEditActivity.getMainPath()),
                true, 0, MediaUtil.getDuration(mVideoEditActivity.getMainPath()), 1.0f);
    }

    @Override
    public void backToMain() {
        mVideoEditActivity.mVideoView.setVideoPath(mVideoEditActivity.getMainPath());
        mVideoEditActivity.mImageView.setImageResource(R.drawable.azant);
        mVideoEditActivity.mVideoView.start();
        mVideoEditActivity.mVideoView.seekTo(1);
        mVideoEditActivity.mode = VideoEditActivity.MODE_NONE;
        mVideoEditActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_video_tailor;
    }

    @Override
    protected void initView() {
        tailorStartTime = 0;
        tailorEndTime = 0;

        mDataBinding.bottomTrackView.setClipVideoMode(TrackModel.ClipVideoMode.OPERATION);
        mDataBinding.bottomTrackView.setMode(TrackModel.ClipMode.CLIP);
        mDataBinding.bottomTrackView.initInfo(mVideoEditActivity.getMainPath(), MediaUtil.getDuration(mVideoEditActivity.getMainPath()),
                true, 0, MediaUtil.getDuration(mVideoEditActivity.getMainPath()), 1.0f);
        mDataBinding.bottomTrackView.getClipVideoTrackView().setBorderColor(Color.parseColor("#FFFFFF"));
        mDataBinding.bottomTrackView.setClipVideoListener(this);
    }

    @Override
    protected void initData() {
        onShow();
    }

    public void applyTailorVideo() {
        if (tailorEndTime == 0 && tailorStartTime == 0) {
            ToastUtils.showShort(R.string.video_tailor_tips);
            return;
        }
        if (mVideoEditActivity.mVideoView.isPlaying()) {
            mVideoEditActivity.mVideoView.pause();
            mVideoEditActivity.mImageView.setImageResource(R.drawable.abof);
        }
        showDialog(getString(R.string.video_tailor_loading) + "0%");
        EditorHolder.editor().videoCut(mVideoEditActivity.getMainPath(), tailorStartTime, tailorEndTime, new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                ToastUtils.showShort(R.string.video_tailor_success);
                tailorStartTime = 0;
                tailorEndTime = 0;
                dismissDialog();
                mVideoEditActivity.changeMainPath(outFilePath, true);
                mDataBinding.bottomTrackView.initInfo(mVideoEditActivity.getMainPath(), MediaUtil.getDuration(mVideoEditActivity.getMainPath()),
                        true, 0, MediaUtil.getDuration(mVideoEditActivity.getMainPath()), 1.0f);
                backToMain();
            }

            @Override
            public void onFailure(String errMsg) {
                dismissDialog();
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(R.string.video_tailor_fail);
            }

            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.video_tailor_loading) + progress + "%");
            }
        });
    }


    @Override
    public void clipLeftSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {
        tailorStartTime = cutStartTime;
        tailorEndTime = cutEndTime;
    }

    @Override
    public void clipRightSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {
        tailorStartTime = cutStartTime;
        tailorEndTime = cutEndTime;
    }

    @Override
    public void cropFirstLeftSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void cropFirstRightSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void cropSecondLeftSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void cropSecondRightSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void splitFirstSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void splitSecondSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }
}